package com.rcklos.NettyLentMe.protocol.handler;

import com.rcklos.NettyLentMe.config.IMServerConfig;
import com.rcklos.NettyLentMe.netty.channel.ChannelPool;
import com.rcklos.NettyLentMe.protocol.interfaces.ServiceInterfaces;
import com.rcklos.NettyLentMe.protocol.protobuf.IMPackage;
import com.rcklos.NettyLentMe.protocol.services.ServicePool;
import io.netty.channel.Channel;

public class ServiceHandler implements ServiceInterfaces {
    private static ServiceHandler instance;

    private IMServerConfig config;

    public static ServiceHandler getInstance() {
        if(instance == null){
            synchronized (ServiceHandler.class){
                instance = new ServiceHandler();
            }
        }
        return instance;
    }

    private ServiceHandler(){
        config = IMServerConfig.getInstance();
    }

    @Override
    public void submitRead(Channel channel, IMPackage.Msg msg) {
        if(msg == null || msg.getHead() == null) return; // msg为空不要
        if(msg.getVersion() != config.getProtocol_version()) return; // 协议版本号不对不要
        IMPackage.Head head = msg.getHead();
        // 校验用户
        if(head.getFrom() != ChannelPool.getInstance().getAttr(channel, ChannelPool.ATTR_KEY_USER_ID))
            return;
        ServicePool.service(head.getType(), msg.getBody(), msg.getExtend());
    }

    @Override
    public void sendMessage(String key, String msg) {

    }


}
